package com.shuqi.database.dao.impl;

import java.sql.SQLException;

import net.sqlcipher.database.SQLiteDatabase;
import android.content.Context;

import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.shuqi.common.utils.Log4an;
import com.shuqi.database.dao.DBConfig;
import com.shuqi.database.dao.OrmBaseDatabaseHelper;
import com.shuqi.database.model.BookBagCatalog;
import com.shuqi.database.model.BookCataLog;
import com.shuqi.database.model.BookInfo;
import com.shuqi.database.model.BookMarkInfo;
import com.shuqi.database.model.SearchHistory;
import com.shuqi.database.model.TxtCatalog;
import com.shuqi.database.model.TxtDownload;
import com.shuqi.database.model.VersionShow;
import com.shuqi.download.database.DownloadInfo;


public class ShuqiDatabaseHelper extends OrmBaseDatabaseHelper {

    private static ShuqiDatabaseHelper mHelper = null;

    public static synchronized ShuqiDatabaseHelper getHelper(Context context) {
        if (mHelper == null) {
            mHelper = new ShuqiDatabaseHelper(context);
        }
        return mHelper;
    }

    public ShuqiDatabaseHelper(Context context) {
        super(context, DBConfig.SHUQI_DB_NAME, null, DBConfig.SHUQI_DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db, ConnectionSource cons) {
        try {
            TableUtils.createTableIfNotExists(cons, BookInfo.class);
            TableUtils.createTableIfNotExists(cons, BookCataLog.class);
            TableUtils.createTableIfNotExists(cons, BookMarkInfo.class);
            TableUtils.createTableIfNotExists(cons, TxtCatalog.class);
            TableUtils.createTableIfNotExists(cons, TxtDownload.class);
            TableUtils.createTableIfNotExists(cons, SearchHistory.class);
            TableUtils.createTableIfNotExists(cons, BookBagCatalog.class);
            TableUtils.createTableIfNotExists(cons, DownloadInfo.class);
            TableUtils.createTableIfNotExists(cons, VersionShow.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource cons, int oldVersion, int newVersion) {
        Log4an.i("ShuqiDatabaseHelper", "onUpgrade: oldVersion=" + oldVersion + ", newVersion="
                + newVersion);
        if (oldVersion == 1) {
            db.execSQL("alter table book_info add update_catalog int;");
            try {
                TableUtils.createTableIfNotExists(cons, DownloadInfo.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (oldVersion == 1 || oldVersion == 2) {
            try {
                TableUtils.createTableIfNotExists(cons, VersionShow.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (oldVersion < 4) {// 7.3.9 升级目录表，增加字段
            db.execSQL("alter table txt_catalog add chapter_index_end int;");
        }
    }
}
